
import { Toast } from 'vant';
import {formatPackTime, formatCourseList} from 'plus/unconfirmPack'
import {classPlan, submitCalendars} from 'api/getData'
import { mapGetters } from 'vuex'
import moment from 'moment'

export default {
    data(){
        return {
            remark:'',
            courseTime: false,
            selectWeekModal: false,
            hasSelectWeek: false,
            weekText: ['日','一','二','三','四','五','六'],
            allDay: '',
            weekArr: [],
            nowWeek: [],
            nowWeekIndex:0,
            canSend: false,
            currentCourse:{},
            startDate:'',
            dataLoading: ''
        }
    },
    computed: {
        ...mapGetters(['currentUser']),
        formatTime(){
            const formatNumber = n => {
              n = n.toString()
              return n[1] ? n : '0' + n
            }
            return (d, sign) => {
              const date = new Date(d)
              const year = date.getFullYear()
              const month = date.getMonth() + 1
              const day = date.getDate()
              const hour = date.getHours()
              const minute = date.getMinutes()
              const second = date.getSeconds()
              // sign 如果0的话返回的是用/来拼接
              // sign 如果1的话返回的是用-来拼接
              return [year, month, day]
                      .map(formatNumber)
                      .join(['/', '-', ':','.'][sign])
            }
        }
    },
    // beforeRouteEnter (to, from, next) {
    //     if(to.query.id && to.query.hcId){
    //             next()
    //     }else{
    //             Toast.fail({
    //             duration: 2000, message:'无效路径'
    //             })
    //             setTimeout(()=>{
    //                 Toast.clear()
    //                 next('/classPlan/classList')
    //             }, 2000)
                
    //     }
    // },
    created(){
        this.dataLoading = this.getLoading('加载中')
        classPlan(this.currentUser.cluesOrStuId).
        then(res=>{
            let courseArr = []
            if(this.$route.query.type == 1) courseArr = formatCourseList(res.freeMapList)
            else courseArr = formatCourseList(res.mapList)
            let filterCourse = [];
            filterCourse = courseArr.filter(item=>{
                return (item.hcId == this.$route.query.hcId && item.id == this.$route.query.id)
            })
            this.currentCourse = filterCourse[0] 
            if(filterCourse.length!=0){
                this.initialData(filterCourse[0].selTimeBegin,filterCourse[0].selTimeEnd)
            }else{
                //未找到对应课程空余时间
                // if(this.$route.meta.isPhone){
                //     Toast.fail({
                //     duration: 2000, message:'未找到对应课程'
                //     })
                //     setTimeout(()=>{
                //         Toast.clear()
                //         this.$router.replace('/H5classList')
                //     }, 2000)
                // }else{ 
                // }
                //pc端跳转
                this.$message({
                    message: '路径无效!',
                    type: 'warning',
                });
                setTimeout(()=>{
                        this.$router.replace('/classList')
                }, 1500)
            }
            this.dataLoading.close()
        }).catch(err=>{
            this.dataLoading.close()
        })
    },
    mounted (){
        
    },
    methods:{
        initialData(start,end){
            this.startDate = moment(start).format('YYYY/MM/DD')
            this.allDay = formatPackTime(start,end)
            this.weekArr = this.formatWeekArr(this.chunkWeek(this.allDay,7))
            this.nowWeek = this.weekArr[0].week
            // console.log(this.allDay)
            // console.log(this.nowWeek)
        },
        hrfeFastSelect(){      
            this.$router.push({
                path: '/H5fastSelect',
                query:{
                    id:this.$route.query.id,
                    hcId:this.$route.query.hcId
                }
            })
        },
        handelSubmit(){
            // console.log(this.canSend)
            if(this.canSend){
                let submitArr = []
                let copyWeek = this.weekArr.slice()
                let course = this.currentCourse.course.id
                let classPlan = this.currentCourse.id
                let stud = this.currentUser.cluesOrStuId
                for(var i=0;i<copyWeek.length;i++){
                    // console.log(this.timeList[i])
                   
                    for(var k=0;k<copyWeek[i]['week'].length;k++){
                        let date = copyWeek[i]['week'][k].year +'/' + copyWeek[i]['week'][k].month +'/' + copyWeek[i]['week'][k].day
                        date = moment(date).format('YYYY-MM-DD')
                        if(copyWeek[i]['week'][k].morning){
                            submitArr.push({
                                courseid: course,
                                classPlanId: classPlan,
                                state: '早',
                                studate: date,
                                stuid: stud
                            })
                        }
                        if(copyWeek[i]['week'][k].afternoon){
                            submitArr.push({
                                courseid: course,
                                classPlanId: classPlan,
                                state: '中',
                                studate: date,
                                stuid: stud
                            })
                        }
                        if(copyWeek[i]['week'][k].evening){
                            submitArr.push({
                                courseid: course,
                                classPlanId: classPlan,
                                state: '晚',
                                studate: date,
                                stuid: stud
                            })
                        }
                    }   
                    
                }
                // console.log(this.remark)
                // console.log(submitArr)
                submitCalendars(this.remark,submitArr).then(res=>{
                    // console.log(res)
                    if(res.code == '0'){
                        if(this.$route.meta.isPhone){
                            this.$router.replace('/H5submitSuccess')
                        }else{
                            this.$router.replace({
                                path:'/webSubmitSuccess',
                                query:{
                                    hcId:this.$route.query.hcId
                                }
                            })
                        } 
                    }
                })
            }
        },
        handelCanSend(){
            let canSend = false
            let copyWeek = this.weekArr.slice()
            for(var i=0;i<copyWeek.length;i++){
                // console.log(this.timeList[i])
                if(!canSend){
                    for(var k=0;k<copyWeek[i]['week'].length;k++){
                        if(copyWeek[i]['week'][k].morning || copyWeek[i]['week'][k].afternoon || copyWeek[i]['week'][k].evening){
                            canSend = true
                            break;
                        }
                    }   
                }
            }
            this.canSend = canSend
        },
        weekEndSelect(){
            let copyWeek = this.weekArr.slice()
            copyWeek.map(item=>{
                if(!item.week[0].disable){
                    item.week[0].morning = true
                    item.week[0].afternoon = true
                    item.week[0].evening = true
                }
                if(!item.week[6].disable){
                    item.week[6].morning = true
                    item.week[6].afternoon = true
                    item.week[6].evening = true
                }
            })
            this.weekArr = copyWeek
            this.canSend = true
        },
        checkEveryWeek(){
            if(this.hasSelectWeek) {
                let copyWeek = this.weekArr.slice()
                copyWeek.map(item=>{
                    if(item.selected){
                        this.nowWeek.map((citem,key)=>{
                            item.week[key].morning = citem.morning
                            item.week[key].afternoon = citem.afternoon
                            item.week[key].evening = citem.evening
                        })
                    }
                })
                this.weekArr = copyWeek
                this.closeSelectWeek()
            }
        },
        selectWeek(disable,key){
            // console.log(disable)
            if(disable) return
            this.weekArr[key].selected = !this.weekArr[key].selected
            for(let i=0;i<this.weekArr.length;i++){
                if(this.weekArr[i].selected){
                    this.hasSelectWeek = true
                    break;
                }else{
                    this.hasSelectWeek = false
                }
            }
        },
        chunkWeek(arr, size) {
            let arr2=[];
            for(let i=0;i<arr.length;i=i+size){
                arr2.push(arr.slice(i,i+size));
            }
            return arr2;
        },
        formatWeekArr(arr){
            let resWeekArr=[]
            arr.map(item=>{
                let disabled = item[0].disable || item[item.length-1].disable
                resWeekArr.push({
                    week:item,
                    disable: disabled,
                    selected: false
                })
            })
            return resWeekArr
        },
        showSelectWeek(){
            let hasSelectDate = false
            this.nowWeek.map(item=>{
                if(!hasSelectDate){
                    hasSelectDate = item.morning || item.afternoon || item.evening
                }
            })
            if(!hasSelectDate) return false
            else return true
            // if(!hasSelectDate) {
            //     Toast('请先选择空闲时间')
            //     return
            // }else{
            //    this.selectWeekModal = true
            // }

        },
        closeSelectWeek(){
            this.selectWeekModal = false
            let copyWeek = this.weekArr.slice()
            copyWeek.map(item=>{
                item.selected = false
            })
            this.hasSelectWeek = false
            this.weekArr = copyWeek
        },
        selectDate(time,key,disable){
            if(disable) return
            this.nowWeek[key][time] = !(this.nowWeek[key][time])
            let copyWeekArr = this.weekArr.slice()
            copyWeekArr[this.nowWeekIndex].week = this.nowWeek
            this.weekArr = copyWeekArr
            this.handelCanSend()
        },
        showCourseTime(bool){
            this.courseTime = bool
        },
        
        addWeek(){
            if(this.nowWeekIndex<this.weekArr.length-1){
                this.nowWeekIndex++;
                this.nowWeek = this.weekArr[this.nowWeekIndex].week
                // console.log(this.nowWeek)
            }
        },
        minusWeek(){
            if(this.nowWeekIndex>0){
                this.nowWeekIndex--;
                this.nowWeek = this.weekArr[this.nowWeekIndex].week
                // console.log(this.nowWeek)
            }
            
        }
    }
}